//倒计时
var countdown=60; 
function settime(obj) { 
    if (countdown == 0) { 
        $(".postKey").attr("check","false");
        obj.innerHTML="获取验证码";
        obj.style.color="#FF6B60" 
        countdown = 60; 
        return;
    } 
    else { 
        $(".postKey").attr("check","true");
        obj.innerHTML="重新发送(" + countdown + ")"; 
        obj.style.color="#999";
        countdown--; 
    } 
    setTimeout(function() { 
    settime(obj) }
    ,1000) 
}

$(".postKey").on("click",function(e){
    $(this).css("border","none")
    var e=e||event;
    e.stopPropagation();
    var phone = $("li").eq(0).find("input").val()
    var type = $("button").attr("data-type")
    if(!$("li").eq(0).find("input").val()){
        $("li").eq(0).next("a").css("display","block").text("手机号不能为空");
        $("li").eq(0).find("input").focus();
    }else{
        if($(this).attr("check") == "false"){
            settime(this);
        }else{
            return;
        }
        $.ajax({
            url: "/novel/user/getDynamicCode",
            type: 'post',
            dataType: 'json',
            data: {mobile: phone,scene:type},
            success:function(data){
                if(data.status != '0'){
                    $("li").eq(2).find("input").attr("data-through","false");
                    $("li").eq(2).next("a").css("display","block").html(data.msg);
                }else{
                    
                }
            },
            error:function(e){
                
            }
        })
//        settime(this);
        console.log(5555)
    }
})

$("li").find("input").on("input propertychange",function(){
    $(this).parent().next("a").css("display","none");
    $(this).css({"border":"1px solid #E4E4E4"})
    
})
//各类验证
$("li").find("input").on("focus",function(){
    var type = $("button").attr('data-type');
    var k=$(this)
    var kIndex=$(this).parent().index();
        $(this).one("blur",function(){
        
        if(!k.val()){
            k.css({"border":"1px solid #FF6B60"})
            switch(kIndex){
                case 0:
                    k.parent().next("a").css("display","block").text("手机号不能为空");
                    break;
                case 2:
                    k.parent().next("a").css("display","block").text("图片验证码不能为空");
                    break;
                case 4:
                    k.parent().next("a").css("display","block").text("短信验证码不能为空");
                    break;
                case 6:
                    k.parent().next("a").css("display","block").text("密码不能为空");
                    break;
                case 8:
                    k.parent().next("a").css("display","block").text("两次密码不一致");
                    break;
                default:
                    break;
            }
            k.attr("data-through","false");
        }else{
            switch(kIndex){
                case 0:
                    var reg = new RegExp( "^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$");
                    var val = $("li").eq(0).find("input").val();
                    $("li").eq(0).next("a").css("display","block").html('');
                    if(reg.test(val)){
                        //手机号输入正确
                        $.ajax({  
                            type : "POST",  //提交方式  
                            url : "../user/verifyPhone",//路径  
                            data : {  
                                mobile : val,
                                type   : type
                            },
                            dataType:'json',
                            success : function(result) {//返回数据根据结果进行相应的处理  
                                console.log(result) 
                                if(result.status != '0'){
                                    k.css({"border":"1px solid #FF6B60"})     
                                    if(result.status == '1002'){
                                        var href = "该手机号已注册，请尝试直接<a href='../user/login'>登录</a>";
                                        $("li").eq(0).next("a").css("display","block").html(href);
                                    }else{
                                        $("li").eq(0).next("a").css("display","block").html(result.msg);
                                    }
                                    k.attr("data-through","false");
                                }else{
                                    k.attr("data-through","true");
                                }
                            }  
                        });
                    }else{
                        k.attr("data-through","false");
                        k.css({"border":"1px solid #FF6B60"});                       
                        $("li").eq(0).next("a").css("display","block").html("手机号输入有误，请重试");
                    }
                    break;
                case 2:
                    var val = $("li").eq(1).find("input").val();
                    k.parent().next("a").css("display","block").text("");
                    $.ajax({  
                        type : "POST",  //提交方式  
                        url : "../user/verifyImgCode",//路径  
                        data : {  
                            imgCode : val
                        },
                        dataType:'json',
                        //数据，这里使用的是Json格式进行传输  
                        success : function(result) {//返回数据根据结果进行相应的处理  
                            if(result.status != '0'){
                                k.attr("data-through","false");
                                k.css({"border":"1px solid #FF6B60"})                       
                                k.parent().next("a").css("display","block").text("图片验证码错误");
                            }else{
                                k.attr("data-through","true");
                            }
                        }  
                    });
                    break;
                case 4:
                    k.attr("data-through","true");
//                    k.css({"border":"1px solid #FF6B60"})   
//                    
//                    k.parent().next("a").css("display","block").text("短信验证码错误");
                    break;
                case 6:
                    var regPassWord = new RegExp("^[a-zA-Z]{1}([a-zA-Z0-9]){4,19}$");
                    var valPass = k.val();
                    if(!regPassWord.test(valPass)){
                        k.attr("data-through","false");
                        k.css({"border":"1px solid #FF6B60"})                       
                        k.parent().next("a").css("display","block").text("密码输入有误，请重试")
                    }else{
                        //密码输入正确
                        k.attr("data-through","true");
                    }
                    break;
                case 8:
                    if($("li").eq(3).find("input").val()){
                        if(k.val()==$("li").eq(3).find("input").val()){
                            //两次密码一致
                            console.log("两次一致")
                            k.attr("data-through","true");
                        }else{
                            k.attr("data-through","false");
                            k.css({"border":"1px solid #FF6B60"})                       
                            k.parent().next("a").css("display","block").text("两次密码不一致");
                        }
                    }else{
                        k.attr("data-through","false");
                        //用户没有输入密码
                        k.css({"border":"1px solid #FF6B60"})                       
                        k.parent().next("a").css("display","block").text("两次密码不一致");
                    }
                    break;
                default:
                    break;                  
            }
        }
    });
});
//用户协议
$(".agreement").on("click",function(){
    location.href="../user/agreement";
});
//点击注册
$("button").on("click",function(){
    var type = $(this).attr('data-type');
    $("li").find("input").trigger("focus",function(){
        $("li").find("input").blur();
        // $(this).trigger("blur");
    });
    for( var i = 0; i < $("li>input").length; i++ ){
        if($("li>input").eq(i).attr("data-through") == "false"){
            return false;
        }
    }
    if(type == '1'){
        var mobile = $("li").eq(0).find("input").val();
        var vcode = $("li").eq(2).find("input").val();
        var password = $("li").eq(3).find("input").val();
        var rePassword = $("li").eq(4).find("input").val();
        $.ajax({  
            type : "POST",  //提交方式  
            url : "../user/register",//路径  
            data : {  
                mobile      : mobile,
                vcode       : vcode,
                password    : password,
                rePassword  : rePassword
            },
            dataType:'json',
            success : function(result) {//返回数据根据结果进行相应的处理  
                console.log(result) 
                if(result.status != '0'){
    //                k.css({"border":"1px solid #FF6B60"})     
                    if(result.status == '1002'){
                        var href = "该手机号已注册，请尝试直接<a href='../user/login'>登录</a>";
                        $("li").eq(0).next("a").css("display","block").html(href);
                    }else if(result.status == '1012'){
                        $("li").eq(2).next("a").css("display","block").html(result.msg);
                    }else if(result.status == '1006' || result.status == '1007' || result.status == '1005' || result.status == '1011'){
                        $("li").eq(3).next("a").css("display","block").html(result.msg);
                        $("li").eq(4).next("a").css("display","block").html(result.msg);
                    }else{
                        $(".message_info_alert>span").html("注册失败，请重试");
                        $(".message_info_alert").fadeIn(function () {
                            setTimeout(function () {
                                $(".message_info_alert").fadeOut()
                                window.location.reload();
                            }, 300)
                        })
                    }
                    return false;
                }else{
                    location.href="../user/login";
                }
            }  
        });
    }else if(type == '2'){
        var mobile = $("li").eq(0).find("input").val();
        var vcode = $("li").eq(2).find("input").val();
        $.ajax({  
            type : "POST",  //提交方式  
            url : "../user/retrieve",//路径  
            data : {  
                mobile      : mobile,
                vcode       : vcode
            },
            dataType:'json',
            success : function(result) {//返回数据根据结果进行相应的处理  
                console.log(result) 
                if(result.status != '0'){
    //                k.css({"border":"1px solid #FF6B60"})     
                    if(result.status == '1014'){
                        $("li").eq(0).next("a").css("display","block").html(result.msg);
                    }else if(result.status == '1012'){
                        $("li").eq(2).next("a").css("display","block").html(result.msg);
                    }else{
                        $(".message_info_alert>span").html("验证失败，请重试");
                        $(".message_info_alert").fadeIn(function () {
                            setTimeout(function () {
                                $(".message_info_alert").fadeOut()
                            }, 300)
                        })
                    }
                    return false;
                }else{
                    location.href="../user/resetPassword?t="+result.data['t'];
                }
            }  
        });
    }
    
});

